package com.billiards.service.impl;

import com.billiards.dao.UserMapper;
import com.billiards.config.PaginationResponse;
import com.billiards.entity.User;
import com.billiards.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
   @Autowired
    private UserMapper userMapper;

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    public int deleteUser(Integer delId) {
        return userMapper.deleteUser(delId);
    }


//    public PageInfo<User> getUserList(Integer page, Integer count) {
//        PageHelper.startPage(page,count);
//        //PageHelper.startPage(page,count);第几页显示几条
//        List<User> userList = userMapper.selectAll();
//        //调用接口里的方法
//        PageInfo<User> UserPageInfo = new PageInfo<>(userList);
//        // 要返回PageInfo<Article>类型的变量，因为没有所以new一个
//        return UserPageInfo;
//    }
 public PaginationResponse<User> getUserList(Integer page, Integer count) {
    PageHelper.startPage(page, count);
    List<User> userList = userMapper.selectAll();
    PageInfo<User> pageInfo = new PageInfo<>(userList);
    PaginationResponse<User> response = new PaginationResponse<>();
    response.setList(userList);
    response.setTotal((int) pageInfo.getTotal());
    return response;
}








    //登录
    @Override
    public String getUserByUserNameAndPassword(User user, HttpSession session) {

        User result = userMapper.getUserByUserNameAndPassword(user);
        System.out.println("===============");
        System.out.println(user);
        System.out.println("===============");
        System.out.println(result);
        if(result!=null){
            session.setAttribute("loginUser",user);
            return "ok";
        }else{
            return "no";
        }
    }






    @Override
    public List<User> getUserUsername(String name) {
        return userMapper.getUserUsername(name);
    }



    // ------------- 9.3 -------------------
    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }
    @Override
    public User findById(int id) {
        User user = userMapper.findById(id);
        return user;
    }
    // --------------------------------------
}
